12. Exercise: Populate the GameViewModel

ANDK L5 16 What Goes In The ViewModel SC V4

Now it’s your turn to complete this exercise yourself.

Let's move code from the GameFragment over to the GameViewModel!

1. Move the word, score, and wordList variables to the GameViewModel:

In the GameFragment find the word, score, and wordList variables, and move them to GameViewModel. Make sure you delete the versions of these variables in GameFragment. Remember, do not move the binding! This is because the binding contains references to views.

2. Move methods resetList, nextWord, onSkip, and onCorrect to the GameViewModel:

In GameFragment, find each of the methods: resetList, nextWord, onSkip and onCorrect. Move them to GameViewModel.

Remove the private modifier onSkip and onCorrect methods so they can be called from the GameFragment.

3. Move the initialization methods to the GameViewModel:

Initialization in the GameFragment involved calling resetList and nextWord. Now that they are both in the GameViewModel, call them in the GameViewModel when it is created.

4. Update the onClickListeners to refer to call methods in the ViewModel, and then update the UI:

Now that onSkip and onCorrect have been moved to the GameViewModel, the OnClickListeners in the GameFragment, refer to method that aren't there. Update the OnClickListeners to call the methods in the GameViewModel. Then in the OnClickListeners, update the score and word texts so that they have the newest data.

5. Update the methods to get word and score from the viewModel:

In the GameViewModel, remove the private modifier from word and score.

In the GameFragment, update gameFinished, updateWordText and updateScoreText to get the data from the gameViewModel.

6. Do final cleanup in the GameViewModel:

Once you've copied over the variables and methods, remove any code that refers to the GameFragment. In the GameViewModel, comment out the reference to gameFinished in nextWord. You'll deal with this later. You can also clean up the log statements from the prior step.

Now run your app, go to the game screen, and change the score. Then rotate your device. Notice how the word and score data is preserved in the view model and no longer resets itself!

Note: The game does not finish because we don’t know how to handle the gameFinished() method. It needs to run from GameFragment because it contains navigation code, but the game finishing is triggered from the view model. We’ll resolve this soon.

If you want to start at this step, you can download this exercise code from: Step.02-Exercise-Populate-the-GameViewModel.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.02-Solution-Populate-the-GameViewModel or using this git diff.

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

Nice job! You can check your solution against the solution we’ve provided here Step.02-Solution-Populate-the-GameViewModel or using this git diff.